﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zj.HttpCodeDemo.Domain.Events;

namespace Zj.HttpCodeDemo.EventStore.Dapper
{
    public class DapperEventStore
        : IEventStore
    {
        private readonly IDbConnection _myConn;
        public DapperEventStore()
        {
            _myConn = new SqlConnection(@"Server=.;Database=TodoDb;Uid=sa;Pwd=Sa1234;Integrated Security=false");
        }
        public void Save<TEvent>(TEvent @event) where TEvent : IEvent
        {
            var descriptor = new EventDescriptor() { Payload = @event.GetType().Name };
            string strSql = string.Empty;//Sample,set it is empty.
            _myConn.Execute(strSql, descriptor);
        }

        public async Task SaveAsync<TEvent>(TEvent @event) where TEvent : IEvent
        {
            await Task.Run(() =>
            {
                Save(@event);
            });
        }
    }
}
